package persistence.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import persistence.dao.CityDao;
import persistence.entity.CityEntity;

public class CityDaoImpl implements CityDao {

	private static final Logger logger = Logger.getLogger(CityDaoImpl.class);

	private static final String CITYDAOIMPL_INFO = "Could not getOrder";
	private static final String CITYDAOIMPL_INFO_INSERT = "Invalid Insert City";
	private static final String CITYDAOIMPL_INFO_DELETE = "Invalid Delete City";
	private static final String CITYDAOIMPL_INFO_GET = "Invalid Get City";
	private static final String CITYDAOIMPL_INFO_UPDATE = "Invalid UpDate City";

	private static final String GET_CITY = "select id_city, name_city from city where name_city like ? ";

	private static final String GET_ALL_CITY = "select id_city, name_city from `city` order by name_city ";

	private static final String INSERT_CITY = "INSERT INTO `city`(name_city) VALUES (?)";

	private static final String DELETE_CITY = "DELETE from `city` WHERE id_city=?";

	private static final String GET_CITY_BY_ID = "select id_city, name_city from city where id_city=? ";

	private static final String SET_CITY_UPDATE = "UPDATE `city` SET name_city = ? where id_city=? ";

	private Connection connection;

	public CityDaoImpl(Connection conn) {
		this.connection = conn;
	}

	@Override
	public Class<CityEntity> getEntityClass() {
		return null;
	}

	@Override
	public List<CityEntity> findAll() {
		return null;
	}

	@Override
	public List<CityEntity> findByExample(CityEntity exampleInstance) {
		return null;
	}

	@Override
	public int countAll() {
		return 0;
	}

	@Override
	public int countByExample(CityEntity exampleInstance) {
		return 0;
	}

	@Override
	public CityEntity insert(CityEntity entity) {
		return null;
	}

	@Override
	public CityEntity update(CityEntity entity) {
		return null;
	}

	@Override
	public CityEntity getCity(String name) {

		CityEntity city = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			stmt = connection.prepareStatement(CityDaoImpl.GET_CITY);
			stmt.setString(1, "%" + name + "%");
			rs = stmt.executeQuery();

			if (rs != null) {
				while (rs.next()) {
					city = new CityEntity();
					city.setId_city(rs.getInt("id_city"));
					city.setName_city(rs.getString("name_city"));
				}
			}
		} catch (Exception e) {
			logger.error(CityDaoImpl.CITYDAOIMPL_INFO + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}

			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return city;
	}

	@Override
	public List<CityEntity> getCity() {

		List<CityEntity> citys = new ArrayList<CityEntity>();
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			stmt = connection.prepareStatement(CityDaoImpl.GET_ALL_CITY);
			rs = stmt.executeQuery();
			while (rs.next()) {
				CityEntity city = new CityEntity();
				city.setId_city(rs.getInt("id_city"));
				city.setName_city(rs.getString("name_city"));
				citys.add(city);
			}

		} catch (Exception e) {
			logger.error(CityDaoImpl.CITYDAOIMPL_INFO + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}

			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return citys;
	}

	@Override
	public CityEntity setCity(String name) {

		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			if (name != "") {
				stmt = connection.prepareStatement(CityDaoImpl.INSERT_CITY);
				stmt.setString(1, name);
				stmt.executeUpdate();
			} else {
				logger.error(CityDaoImpl.CITYDAOIMPL_INFO_INSERT);
			}
		} catch (Exception e) {
			logger.error(CityDaoImpl.CITYDAOIMPL_INFO + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return null;
	}

	@Override
	public CityEntity delete(int[] id_city) {

		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			if (id_city != null) {
				for (int i = 0; i < id_city.length; i++) {
					stmt = connection.prepareStatement(CityDaoImpl.DELETE_CITY);
					stmt.setInt(1, id_city[i]);
					stmt.executeUpdate();
				}
			} else {
				logger.error(CityDaoImpl.CITYDAOIMPL_INFO_DELETE);
			}
		} catch (Exception e) {
			logger.error(CityDaoImpl.CITYDAOIMPL_INFO + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return null;
	}

	@Override
	public List<CityEntity> getCityById(int[] id_city) {

		List<CityEntity> cityUp = new ArrayList<CityEntity>();
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			if (id_city != null) {
				for (int i = 0; i < id_city.length; i++) {
					stmt = connection.prepareStatement(CityDaoImpl.GET_CITY_BY_ID);
					stmt.setInt(1, id_city[i]);
					rs = stmt.executeQuery();
					rs.next();
					CityEntity city = new CityEntity();
					city.setId_city(rs.getInt("id_city"));
					city.setName_city(rs.getString("name_city"));
					cityUp.add(city);
				}
			} else {
				logger.error(CityDaoImpl.CITYDAOIMPL_INFO_GET);
			}
		} catch (Exception e) {
			logger.error(CityDaoImpl.CITYDAOIMPL_INFO + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return cityUp;
	}

	@Override
	public CityEntity setCityUpdate(int[] id_city, String[] name) {

		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			if (id_city != null) {
				for (int i = 0; i < id_city.length; i++) {
					stmt = connection.prepareStatement(CityDaoImpl.SET_CITY_UPDATE);
					stmt.setString(1, name[i]);
					stmt.setInt(2, id_city[i]);
					stmt.executeUpdate();
				}
			} else {
				logger.error(CityDaoImpl.CITYDAOIMPL_INFO_UPDATE);
			}
		} catch (Exception e) {
			logger.error(CityDaoImpl.CITYDAOIMPL_INFO + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return null;
	}
}
